package meshGrammar;

import processing.core.PGraphics;
import processing.core.PVector;

public class MeshSpring {
	MeshNode n1;
	MeshNode n2;
	float desiredDist = 10f;
	float force = 0.2f;//0.2f
	
	public float getDesiredDist() {
		return desiredDist;
	}

	public void setDesiredDist(float desiredDist) {
		this.desiredDist = desiredDist;
	}


	public MeshSpring(MeshNode n1, MeshNode n2) {
		this.n1 = n1;
		this.n2 = n2;
	}

	public float getStress(){
		return this.getLength() - this.getDesiredDist();
	}
	
	public float getLength() {
		return n1.dist(n2);
	}

	public void display2D(PGraphics g){
		g.line(n1.x,n1.y,n2.x,n2.y);
	}
	public MeshNode getOtherNode(MeshNode n){
		if (n1.equals(n))return n2;
		return n1;
	}
	
	public void adapt() {
		float dist = getLength();
		Vec3D v = Vec3D.sub(n1, n2);
		//System.out.println("dist: "+dist);
		float delta = dist - this.getDesiredDist();
		//System.out.println(delta);
		if (Math.abs(delta) >0.00f) {
			Vec3D f1 = Vec3D.mult(v,  (delta / dist) * force);
			Vec3D f2 = Vec3D.mult(f1, -1);
			n1.add(f2);
			n2.add(f1);
		}
	}

}